/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogoActualizarClave.java
 *
 * Created on 26-abr-2013, 12:23:24
 */
package com.camachosanchez.gui;

import com.camachosanchez.bean.Usuarios;
import com.camachosanchez.controler.jpa.UsuariosJpaController;
import com.camachosanchez.util.Constantes;
import com.camachosanchez.util.GUIUtil;
import com.camachosanchez.util.SQLUtil;
import com.camachosanchez.util.seguridad.Encriptador;
import javax.swing.JOptionPane;

/**
 *
 * @author Rolando
 */
public class DialogoActualizarClave extends javax.swing.JDialog {

    /** Creates new form DialogoActualizarClave */
    public DialogoActualizarClave(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        GUIUtil.addEscapeKey(this);
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlContenedorSuperior = new javax.swing.JPanel();
        pnlContenedor = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        txtPassActual = new javax.swing.JPasswordField();
        txtPassNueva = new javax.swing.JPasswordField();
        txtRePassNueva = new javax.swing.JPasswordField();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(".:: Cambiar contraseña ::.");
        setResizable(false);

        pnlContenedorSuperior.setBackground(new java.awt.Color(231, 239, 247));

        pnlContenedor.setLayout(new java.awt.BorderLayout(4, 4));

        jPanel2.setLayout(new java.awt.GridLayout(3, 1, 4, 4));

        jLabel1.setText("Contraseña actual:");
        jPanel2.add(jLabel1);

        jLabel2.setText("Nueva contraseña:");
        jPanel2.add(jLabel2);

        jLabel3.setText("Repita la nueva contraseña:");
        jPanel2.add(jLabel3);

        pnlContenedor.add(jPanel2, java.awt.BorderLayout.LINE_START);

        jPanel3.setLayout(new java.awt.GridLayout(3, 1, 4, 4));
        jPanel3.add(txtPassActual);
        jPanel3.add(txtPassNueva);
        jPanel3.add(txtRePassNueva);

        pnlContenedor.add(jPanel3, java.awt.BorderLayout.CENTER);

        btnGuardar.setBackground(new java.awt.Color(255, 255, 204));
        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/camachosanchez/recursos/16_save.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        btnGuardar.setContentAreaFilled(false);
        btnGuardar.setOpaque(true);
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setBackground(new java.awt.Color(255, 255, 204));
        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/camachosanchez/recursos/16_cancel.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        btnCancelar.setContentAreaFilled(false);
        btnCancelar.setOpaque(true);
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlContenedorSuperiorLayout = new javax.swing.GroupLayout(pnlContenedorSuperior);
        pnlContenedorSuperior.setLayout(pnlContenedorSuperiorLayout);
        pnlContenedorSuperiorLayout.setHorizontalGroup(
            pnlContenedorSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlContenedorSuperiorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlContenedorSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlContenedorSuperiorLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnGuardar)
                        .addGap(13, 13, 13)
                        .addComponent(btnCancelar))
                    .addComponent(pnlContenedor, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlContenedorSuperiorLayout.setVerticalGroup(
            pnlContenedorSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlContenedorSuperiorLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(pnlContenedor, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlContenedorSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        getContentPane().add(pnlContenedorSuperior, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
    this.setVisible(false);
    dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
    try {
        // TODO add your handling code here:
        char[] claveActual = txtPassActual.getPassword();
        char[] nuevaClave = txtPassNueva.getPassword();
        char[] nuevaClaveRepetida = txtRePassNueva.getPassword();
        String strClaveActual = "";
        String strNuevaClave = "";
        String strNuevaClaveRepetida = "";
        for (int i = 0; i < claveActual.length; i++) {
            strClaveActual += claveActual[i];
        }
        for (int i = 0; i < nuevaClave.length; i++) {
            strNuevaClave += nuevaClave[i];
        }
        for (int i = 0; i < nuevaClaveRepetida.length; i++) {
            strNuevaClaveRepetida += nuevaClaveRepetida[i];
        }
        if (strClaveActual.isEmpty()) {
            JOptionPane.showMessageDialog(Principal.getInstance(), "Ingrese la contraseña actual.", "Error", JOptionPane.ERROR_MESSAGE);
            txtPassActual.requestFocusInWindow();
            return;
        }
        if (strNuevaClave.isEmpty()) {
            JOptionPane.showMessageDialog(Principal.getInstance(), "La nueva contraseña no puede ser vacía.", "Error", JOptionPane.ERROR_MESSAGE);
            txtPassNueva.requestFocusInWindow();
            return;
        }
        if (strNuevaClaveRepetida.isEmpty() || strNuevaClave.compareTo(strNuevaClaveRepetida) != 0) {
            JOptionPane.showMessageDialog(Principal.getInstance(), "Las nuevas contraseñas ingresadas no coinciden.", "Error", JOptionPane.ERROR_MESSAGE);
            txtPassNueva.requestFocusInWindow();
            return;
        }
        if (SQLUtil.autenticar(Constantes.currentUser.getNombreAcceso(), Encriptador.encriptar(strClaveActual, Constantes.LLAVE)) != null) {
            Usuarios nuevoUsuario = Constantes.currentUser;
            nuevoUsuario.setClaveAcceso(Encriptador.encriptar(strNuevaClave, Constantes.LLAVE).getBytes());
            UsuariosJpaController uc = new UsuariosJpaController(null);
            uc.edit(nuevoUsuario);
            JOptionPane.showMessageDialog(Principal.getInstance(), "Se actualizó la contraseña correctamente.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            dispose();
        } else {
            JOptionPane.showMessageDialog(Principal.getInstance(), "La contraseña actual es incorrecta.", "Error", JOptionPane.ERROR_MESSAGE);
            txtPassActual.requestFocusInWindow();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}//GEN-LAST:event_btnGuardarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel pnlContenedor;
    private javax.swing.JPanel pnlContenedorSuperior;
    private javax.swing.JPasswordField txtPassActual;
    private javax.swing.JPasswordField txtPassNueva;
    private javax.swing.JPasswordField txtRePassNueva;
    // End of variables declaration//GEN-END:variables
}
